
package com.intel.factorytest.activity;

import android.view.View;
import android.util.Log;
import android.util.AttributeSet;
import android.graphics.Shader;
import android.graphics.Rect;
import android.graphics.Paint;
import android.graphics.LinearGradient;
import android.graphics.Color;
import android.graphics.Canvas;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.content.res.Resources;
import android.content.Context;

import com.intel.factorytest.R;

public class FihDoodleView extends View{
    private static final int COLOR[]={Color.WHITE,Color.BLACK,Color.RED,
        Color.BLUE,Color.GREEN};
    private static final int MTRANSX = 5;
    public int mIndex = 0;
    private Shader mShader;
    private Paint mPaint;
    private boolean isShowAll = false;
    private int mWidth = 0;
    private int mHeight = 0;
    private boolean isDrawAll = true;
    private Resources mRes;
    private int mPreTransX = 0 ;
    
    private boolean mAdd = false;
    public FihDoodleView(Context context) {
        super(context);
        mRes = context.getResources();
    }
    public FihDoodleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mShader = new LinearGradient(0, 0, 200, 0, new int[] {
                Color.RED, Color.GREEN, Color.BLUE },
                null, Shader.TileMode.MIRROR);
        mPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
        mRes = context.getResources();
    }
    
    public void setWidthHeight(int width,int height){
        mWidth = width;
        mHeight = height;
    }
    
    @Override
    public void draw(Canvas canvas) {
        System.out.println("tangjun display: called draw()");
        if(isShowAll){
            drawDynamic(canvas);
        }else{
            if(isDrawAll){
                System.out.println("tangjun display: mIndex=" + mIndex + ",isDrawAll=" + isDrawAll);
                drawAllColor(canvas);
            }else{
                drawThreeColor(canvas);
            }
        }
        super.draw(canvas);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
        Log.v(DisplayStatusDisplayTest.TAG,"widthMeasureSpec = " + widthMeasureSpec + "   heightMeasureSpec = " +  heightMeasureSpec + " mWidth = " + mWidth);
        setMeasuredDimension(mWidth*2, mHeight*2);
    //    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    public void drawDynamic(Canvas canvas){
        mPaint.setShader(mShader);
        Log.v(DisplayStatusDisplayTest.TAG,"mPreTransX = "+mPreTransX);
        canvas.translate(mPreTransX, 0);
        canvas.drawRect(0, 0, mWidth*3, mHeight, mPaint);
        if(mAdd){
            mPreTransX += MTRANSX;
        }else{
            mPreTransX -= MTRANSX;
        }
        
        if(mPreTransX < -640){
            mPreTransX = -640;
            mAdd = true;
        }
        
        if(mPreTransX > 0){
            mPreTransX = 0;
            mAdd = false;
        }
    }
    
    public void drawAllColor(Canvas canvas){
        Bitmap bitmap = null;
        switch(mIndex){
        case 0:
            bitmap = BitmapFactory.decodeResource(mRes, R.drawable.whitedisplay);
            break;
        /*
        case 1:
            bitmap = BitmapFactory.decodeResource(mRes, R.drawable.graydisplay);
            break;
        */
        /*case 1:
            bitmap = BitmapFactory.decodeResource(mRes, R.drawable.blackdisplay);
            break;*/
        case 1:
            bitmap = BitmapFactory.decodeResource(mRes, R.drawable.bluedisplay);
            break;
        case 2:
            bitmap = BitmapFactory.decodeResource(mRes, R.drawable.greendisplay);
            break;
        case 3:
            bitmap = BitmapFactory.decodeResource(mRes, R.drawable.reddisplay);
            break;
        default :
                break;
        }
        if(bitmap != null){
            canvas.drawBitmap(bitmap, null, new Rect(0, 0, (int)(mWidth*1.5), (int)(mHeight*1.5)), mPaint);
        }
        
        mIndex++;
        if(mIndex>=4){
            mIndex = 0;
            isDrawAll = false;
    //yjcui        mPaint.setStrokeWidth(2);
        }
    }
    
    public void drawThreeColor(Canvas canvas){
        //1
        checkIndex();
        mPaint.setColor(COLOR[mIndex]);
        canvas.drawRect(0, 0, mWidth/3, mHeight, mPaint);
        
        //2
        mIndex++;
        checkIndex();
        mPaint.setColor(COLOR[mIndex]);
        canvas.drawRect(mWidth/3, 0, 2*mWidth/3, mHeight, mPaint);
        
        //3
        mIndex++;
        checkIndex();
        mPaint.setColor(COLOR[mIndex]);
        canvas.drawRect(2*mWidth/3, 0, 320, mHeight, mPaint);
        
        if(mIndex==0){
            mIndex = COLOR.length-1;
            
        }else if(mIndex==1){
            isShowAll = true;
        }else{
            mIndex--;
        }
    }
    
    public void checkIndex(){
        if(mIndex > COLOR.length-1){
            mIndex = 0;
        }
    }
    
    public boolean getShowAnimation(){
        return isShowAll;
    }
    
    public void setDrawAll(boolean isDrawAll) {
        this.isDrawAll = isDrawAll;
    }
    
    public boolean getShowDone(){
        return isDrawAll;
    }
}
